var ctx=window.daijia.ctx;
var main=window.daijia.main;
$(document).ready(function(){
	
	$("#startTime").datetimepicker({
        format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	$("#endTime").datetimepicker({
		format: "yyyy-mm-dd HH:ii",
        language: 'zh-CN',
        showMeridian: true,
        autoclose: true,
        todayBtn: true
    });
	
	var newDate = new Date();
	var startDate = new Date();
	startDate.setDate(startDate.getDate()-30);
	var startTimeStr=startDate.getFullYear()+"-"+((startDate.getMonth()+1)>9?(startDate.getMonth()+1):'0' + (startDate.getMonth()+1))+"-"+(startDate.getDate()>9?startDate.getDate():'0' + startDate.getDate())+" "+"00:00";
	var endTimeStr=newDate.getFullYear()+"-"+((newDate.getMonth()+1)>9?(newDate.getMonth()+1):'0' + (newDate.getMonth()+1))+"-"+(newDate.getDate()>9?newDate.getDate():'0' + newDate.getDate())+" "+"23:59";
	$("#startTime").val(startTimeStr);
	$("#endTime").val(endTimeStr);
	
	$(".table tbody td").each(function(i,td){
		if($(td).attr("id")!=undefined){
			$(td).html('<img src="'+ctx+'/assets/img/jiazai-094.gif">');
		}
	});
	loadData();
	
	$("#companyId").change(function(){
		$(".table tbody td").each(function(i,td){
			if($(td).attr("id")!=undefined){
				$(td).html('<img src="'+ctx+'/assets/img/jiazai-094.gif">');
			}
		});
		loadData();
	});
	
	//点击搜索
	$("#search_button").click(function(){
		if($("#startTime").val()=="" || $("#endTime").val()==""){
			$.scojs_message("日期不能为空", $.scojs_message.TYPE_ERROR);
			return false;
		}
		var DATE_FORMAT = /^[0-9]{4}-[0-1]{1}[0-9]{1}-[0-3]{1}[0-9]{1} [0-2]{1}[0-9]{1}:[0-5]{1}[0-9]{1}$/;
		if(!DATE_FORMAT.test($("#startTime").val()) || !DATE_FORMAT.test($("#endTime").val())){
		   $.scojs_message("您输入的日期格式有误，正确格式应为：2012-01-01 08:30", $.scojs_message.TYPE_ERROR);
		   return false;
		}
		if($("#startTime").val()>$("#endTime").val()){
			 $.scojs_message("开始日期不能大于结束日期", $.scojs_message.TYPE_ERROR);
			 return false;
		}
		
		$("#dateRangeText").text($("#startTime").val()+" 至 "+$("#endTime").val());
		$("#dateRangeButton").parent().find(".dropdown-calendar").hide();
		
		$(".table tbody td").each(function(i,td){
			if($(td).attr("id")!=undefined){
				$(td).html('<img src="'+ctx+'/assets/img/jiazai-094.gif">');
			}
		});
		loadData();
	});
	$("#export_button").click(function(){
		var companyAll=$("input[name=companyAll]").prop("checked");
		if(main){
			if(companyAll){
				window.location=ctx+"/chart/exportPaotuiCompositiveData?"+"&startTime="+$("#startTime").val()+
	            "&endTime="+$("#endTime").val();
			}else{
				window.location=ctx+"/chart/exportPaotuiCompositiveData?"+"&startTime="+$("#startTime").val()+
                "&endTime="+$("#endTime").val()+"&companyId="+$("#companyId").val();
			}
		}else{
			window.location=ctx+"/chart/exportPaotuiCompositiveData?"+"&startTime="+$("#startTime").val()+
            "&endTime="+$("#endTime").val();
		}
	});
	
});

function loadData(){
	
	$.ajax({
        url : ctx+'/chart/paotuiCompositiveData',
        type:'GET',
        async:true,
        dataType : 'json',
        data : {
        	"startTime":$("#startTime").val(),
        	"endTime":$("#endTime").val(),
        	"companyId":$("#companyId").val()
        },
        success : function(result){
        	
        	
        	$("#companyName").html(result.companyName);
        	
    		$("#orderFinishedAmount_this").html(result.orderFinishedAmount_this);
    		$("#orderBusyAmount_this").html(result.orderBusyAmount_this);
    		$("#orderFinishedAmount_getout").html(result.orderFinishedAmount_getout);
    		$("#orderBusyAmount_getout").html(result.orderBusyAmount_getout);
    		$("#orderCanceledAmount").html(result.orderCanceledAmount);
    		$("#orderFinishedAmount_comein").html(result.orderFinishedAmount_comein);
    		$("#orderBusyAmount_comein").html(result.orderBusyAmount_comein);
    		$("#orderTotalAmount").html(result.orderTotalAmount);
    		
    		$("#orderFinishedMoney_this").html(result.orderFinishedMoney_this);
    		$("#orderBusyMoney_this").html(0);
    		$("#orderFinishedMoney_getout").html(result.orderFinishedMoney_getout);
    		$("#orderBusyMoney_getout").html(0);
    		$("#orderCanceledMoney").html(0);
    		$("#orderFinishedMoney_comein").html(result.orderFinishedMoney_comein);
    		$("#orderBusyMoney_comein").html(0);
    		$("#orderTotalMoney").html(result.orderTotalMoney);
    		
    		$("#orderFinishedCouponMoney_this").html(0);
    		$("#orderBusyCouponMoney_this").html(0);
    		$("#orderFinishedCouponMoney_getout").html(0);
    		$("#orderBusyCouponMoney_getout").html(0);
    		$("#orderCanceledCouponMoney").html(0);
    		$("#orderFinishedCouponMoney_comein").html(0);
    		$("#orderBusyCouponMoney_comein").html(0);
    		$("#orderTotalCouponMoney").html(0);
    		
    		$("#orderFinishedPayMoney_this").html(result.orderFinishedMoney_this);
    		$("#orderBusyPayMoney_this").html(0);
    		$("#orderFinishedPayMoney_getout").html(result.orderFinishedMoney_getout);
    		$("#orderBusyPayMoney_getout").html(0);
    		$("#orderCanceledPayMoney").html(0);
    		$("#orderFinishedPayMoney_comein").html(result.orderFinishedMoney_comein);
    		$("#orderBusyPayMoney_comein").html(0);
    		$("#orderTotalPayMoney").html(result.orderTotalMoney);
    		
    		$("#orderFinishedCompanyMoney_this").html(result.orderFinishedCompanyMoney_this);
    		$("#orderBusyCompanyMoney_this").html(0);
    		$("#orderFinishedCompanyMoney_getout").html(result.orderFinishedCompanyMoney_getout);
    		$("#orderBusyCompanyMoney_getout").html(0);
    		$("#orderCanceledCompanyMoney").html(0);
    		$("#orderFinishedCompanyMoney_comein").html(result.orderFinishedCompanyMoney_comein);
    		$("#orderBusyCompanyMoney_comein").html(0);
    		$("#orderTotalCompanyMoney").html(result.orderTotalCompanyMoney);
    		$("#orderFinishedDriverMoney_this").html(result.orderFinishedDriverMoney_this);
    		$("#orderBusyDriverMoney_this").html(0);
    		$("#orderFinishedDriverMoney_getout").html(result.orderFinishedDriverMoney_getout);
    		$("#orderBusyDriverMoney_getout").html(0);
    		$("#orderCanceledDriverMoney").html(0);
    		$("#orderFinishedDriverMoney_comein").html(result.orderFinishedDriverMoney_comein);
    		$("#orderBusyDriverMoney_comein").html(0);
    		$("#orderTotalDriverMoney").html(result.orderTotalDriverMoney);
    		
    		$("#orderFinishedInfofee_this").html(0);
    		$("#orderBusyInfofee_this").html(0);
    		$("#orderFinishedInfofee_getout").html(0);
    		$("#orderBusyInfofee_getout").html(0);
    		$("#orderCanceledInfofee").html(0);
    		$("#orderFinishedInfofee_comein").html(0);
    		$("#orderBusyInfofee_comein").html(0);
    		$("#orderTotalInfofee").html(0);
    		
    		$("#orderFinishedManagerMoney_this").html(0);
    		$("#orderBusyManagerMoney_this").html(0);
    		$("#orderFinishedManagerMoney_getout").html(0);
    		$("#orderBusyManagerMoney_getout").html(0);
    		$("#orderCanceledManagerMoney").html(0);
    		$("#orderFinishedManagerMoney_comein").html(0);
    		$("#orderBusyManagerMoney_comein").html(0);
    		$("#orderTotalManagerMoney").html(0);
    		
    		$("#orderFinishedRebateMoney_this").html(0);
    		$("#orderBusyRebateMoney_this").html(0);
    		$("#orderFinishedRebateMoney_getout").html(0);
    		$("#orderBusyRebateMoney_getout").html(0);
    		$("#orderCanceledRebateMoney").html(0);
    		$("#orderFinishedRebateMoney_comein").html(0);
    		$("#orderBusyRebateMoney_comein").html(0);
    		$("#orderTotalRebateMoney").html(0);
    		
    		if(main){
    			$("#totalAmount").html(result.totalAmount);
    			$("#totalGuoluMoney").html(result.totalGuoluMoney);
        		$("#totalCancelAmount").html(result.totalCancelAmount);
        		$("#totalRealCash").html(result.totalRealCash);
        		$("#totalCouponMoney").html(result.totalCouponMoney);
        		$("#totalPayMoney").html(result.totalPayMoney);
        		$("#totalCompanyMoney").html(result.totalCompanyMoney);
        		$("#totalDriverMoney").html(result.totalDriverMoney);
        		$("#totalManagerMoney").html(0);
        		$("#totalInfofee").html(0);
        		$("#totalRebateMoney").html(0);
    		}
        	
        }
	});

}





